﻿using Prism.Commands;
using Prism.Services.Dialogs;
using SmartParking.Entities;
using SmartParking.IService;
using SmartParking.Models;
using SmartParking.ORM.SmartParkingSqlLiteEntity;
using SmartParking.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Navigation;

namespace SmartParking.ViewModels.Pages.Dialogs
{
    public class ModifyMenuViewModel : DialogViewModelBase
    {
        public int SelectedParentId { get; set; }
        public MenuItemModel MenuItem { get; set; } = new();
        public List<SysMenu> ParentNodes { get; set; } = [];


        IMenuService _menuService;
        public ModifyMenuViewModel(IMenuService menuService)
        {
            _menuService = menuService;

        }

        public override void OnDialogOpened(IDialogParameters parameters)
        {
            ParentNodes = _menuService.Query<SysMenu>(t => t.MenuType == 1).ToList();
            ParentNodes.Insert(0, new SysMenu { Id = 0, MenuHeader = "Root" });

            var model = parameters.GetValue<MenuItemModel>("model");
            if (model == null)
            {
                Title = "新建菜单项";

                MenuItem.ParentId = 0;
                MenuItem.MenuType = 1;
            }
            else
            {
                Title = "修改菜单项";

                var cModel = _menuService.Find<SysMenu>(model.MenuId);

                MenuItem.MenuId = cModel.Id;
                MenuItem.MenuHeader = cModel.MenuHeader ?? "";
                MenuItem.MenuIcon = cModel.MenuIcon ?? "";
                MenuItem.ParentId = cModel.ParentId ?? 0;
                MenuItem.TargetView = cModel.TargetView ?? "";
                MenuItem.MenuType = cModel.MenuType ?? 1;
            }
        }

        public override void DoSave() 
        {
            try
            {
                if (MenuItem.MenuId == 0)
                {
                    int newId = _menuService.GetLastId() + 1;

                    _menuService.Insert<SysMenu>(new SysMenu 
                    {
                        Id = newId,
                        MenuHeader = MenuItem.MenuHeader,
                        MenuIcon = MenuItem.MenuIcon,
                        ParentId = MenuItem.ParentId,
                        TargetView = MenuItem.TargetView,
                        MenuType = MenuItem.MenuType,
                        Index = _menuService.GetMenuCount(),
                        State = 1,
                    });
                }
                else
                {
                    
                    var data = _menuService.Find<SysMenu>(MenuItem.MenuId);

                    data.MenuHeader = MenuItem.MenuHeader;
                    data.MenuIcon = MenuItem.MenuIcon;
                    data.ParentId = MenuItem.ParentId;
                    data.TargetView = MenuItem.TargetView;
                    data.MenuType = MenuItem.MenuType;

                    _menuService.Update<SysMenu>(data);
                }

                base.DoSave();
            }
            catch (Exception ex) 
            {
                MessageBox.Show(ex.Message);
            }
        }

        
    }
}
